require "pathname"
require "cri"

class Sweet
	def initialize depth=3
		@base=Pathname.pwd
		@depth=depth
		@base_level=@base.each_filename.to_a.length
	end
	#this is trivial,and ad-hoc hack, its not that pretty
	#it can be easily replace though with a better alternative
	def publish path 
		dir_level=path.each_filename.to_a.length
		level=dir_level-@base_level
		puts "|   "*level+"|-->"+path.basename.to_s
	end

    def walk path=@base
    	@path=path
    	@level=@path.each_filename.to_a.length-@base_level
    	if @depth-@level==0
    		if @path.file?
    			self.publish(@path)
    		elsif @path.directory?    				
    		    @path.each_child do |child|
    			    self.publish(child)
    		    end
    	    end
    	elsif @level>=0 and @level<@depth
    		if @path.file?
    			self.publish(@path)
    		elsif @path.directory?    			
    			self.publish(@path)
    			@path.each_child do |child|
    				self.walk(child)
    			end
    		end
    	end
    end   			
end

#the commandline tool
command=Cri::Command.define do
	name    "sweet"
	usage    "sweet [depth]"
	aliases    :sw,:swe
	summary  "views youur directory contents"
	description <<-EOS
	THis command prints the contents of a directory in a tree like structure
	currently depth is not yet supposrted it is set to 3 by default.
	to use just type sweet.
	EOS

	flag :h,:help,"shows how to use the command" do |value,cmd|
		puts cmd.help
		exit 0
	end

	run do |opts,args,cmd|
		if args.empty?
			sweet_baby=Sweet.new
			sweet_baby.walk
		elsif argslength==1 and args[0]<4
			sweet_baby=Sweet.new(args[0])
			sweet_baby.walk
		else			
			puts <<-EOS
			Too deep to travel
        	EOS
	
		end
	end
end

command.run(ARGV)



